<?php
class DataLogger{
	public function recordProfileChanges($oldProfile, $newProfile){
		//record changes of profile attributes
		$this->recordProfileAttributesChange($oldProfile, $newProfile);
		//record changes of profile academy
		$this->recordProfileAcademyChanges($oldProfile, $newProfile);
		//record changes of profile family
		$this->recordProfileFamilyChanges($oldProfile, $newProfile);
	}
	
	public function recordProfileAttributesChange($oldProfile, $newProfile){
		foreach($oldProfile->attributes as $i => $attProfile){
			if($attProfile != $newProfile->attributes[$i]){
				if($i != "update_date"){
					$history = new History("create");
					$history->unsetAttributes();
					$history->field_name = $oldProfile->getAttributeLabel($i);
					$history->old_value = $attProfile;
					$history->new_value = $newProfile->attributes[$i];
					$history->module = "Profile";
					if($oldProfile->language != "VN"){
						$history->action = "Translate";
						$history->object_id = $oldProfile->related_id;
					}else{
						$history->action = "Update";
						$history->object_id = $oldProfile->id;
					}
					$history->save();
				}
			}
		}
	}
	
	public function recordProfileAcademyChanges($oldProfile, $newProfile){
		foreach($oldProfile->academy->attributes as $i => $attAcademy){
			if($attAcademy != $newProfile->academy->attributes[$i]){
				if($i != "update_date"){
					$history = new History("create");
					$history->unsetAttributes();
					$history->field_name = $oldProfile->academy->getAttributeLabel($i);
					$history->old_value = $attAcademy;
					$history->new_value = $newProfile->academy->attributes[$i];
					$history->module = "Profile";
					if($oldProfile->language != "VN"){
						$history->action = "Translate";
						$history->object_id = $oldProfile->related_id;
					}else{
						$history->action = "Update";
						$history->object_id = $oldProfile->id;
					}
					$history->save();
				}
			}
		}
	}
	
	public function recordProfileFamilyChanges($oldProfile, $newProfile){
		foreach($oldProfile->family->attributes as $i => $attFamily){
			if($attFamily != $newProfile->family->attributes[$i]){
				if($i != "update_date"){
					$history = new History("create");
					$history->unsetAttributes();
					$history->field_name = $oldProfile->family->getAttributeLabel($i);
					$history->old_value = $attFamily;
					$history->new_value = $newProfile->family->attributes[$i];
					$history->module = "Profile";
					if($oldProfile->language != "VN"){
						$history->action = "Translate";
						$history->object_id = $oldProfile->related_id;
					}else{
						$history->action = "Update";
						$history->object_id = $oldProfile->id;
					}
					$history->save();
				}
			}
		}
	}
}
?>